package uk.co.cdal.cobweb.shared;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

import javax.swing.ImageIcon;

import uk.co.cdal.cobweb.backend.BackendImpl;
import uk.co.cdal.cobweb.frontend.Frontend;

public class Idea implements Serializable {
	
	private Idea parent;
	
	private ArrayList<Idea> children;
	
	private String id;
	
	private String title;
	
	private String text;
	
	private ArrayList<String> urls;
	
	private ArrayList<ImageIcon> images;
	
	private boolean changed;
	
	private int nodeCounter;
	
	public Idea() {
		init();
	}
	
	public Idea(Idea parent) {
		init();
		this.parent = parent;
	}
	
	/**
	 * 
	 * @param i The idea to check parentage against
	 * @return Whether the given idea is this ideas parent
	 */
	public boolean isParent(Idea i) {
		if(i.equals(getParent())) {
			return true;
		}
		return false;
	}
	
	/**
	 * @return the children
	 */
	public ArrayList<Idea> getChildren() {
		return children;
	}

	/**
	 * @param children the children to set
	 */
	public void addChild(Idea child) {
		children.add(child);
	}
	
	public void removeChild(Idea child) {
		children.remove(child);
	}

	/**
	 * @return the parent
	 */
	public Idea getParent() {
		return parent;
	}

	/**
	 * @param parent the parent to set
	 */
	public void setParent(Idea parent) {
		this.parent = parent;
	}
	
	/**
	 * Common code that both constructors use
	 *
	 */
	private void init() {
		children = new ArrayList<Idea>();
		parent = null;
		nodeCounter = 0;
		try {
			this.id = InetAddress.getLocalHost().getHostAddress() + Frontend.getFrontend().getBackend().getNodeCount();
		}
		catch(UnknownHostException e) {
			BackendImpl.log.severe("Cannont resolve localhost");
		}
	}
	
	private int getNextNodeCounter() {
		nodeCounter++;
		return nodeCounter - 1;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the furtherText
	 */
	public String getFurtherText() {
		return text;
	}

	/**
	 * @param furtherText the furtherText to set
	 */
	public void setFurtherText(String text) {
		this.text = text;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}
	
	/**
	 * Add a string representing the images location to the list
	 * @param image the string representing the images location
	 */
	public void addImage(ImageIcon image) {
		images.add(image);
	}
	
	/**
	 * Remove an image location from the list
	 * @param image The image to be removed from the list
	 */
	public void removeImage(ImageIcon image) {
		images.remove(image);
	}
	
	/**
	 * Add a URL to the list of URL's associated with this node.
	 * @param URL the URL to be added
	 */
	public void addURL(String URL) {
		urls.add(URL);
	}
	
	/**
	 * Remove a URL from the list of URLS
	 * @param URL the URL to remove.
	 */
	public void removeURL(String URL) {
		urls.remove(URL);
	}

	/**
	 * @return the images
	 */
	public ArrayList<ImageIcon> getImages() {
		return images;
	}

	/**
	 * @param images the images to set
	 */
	public void setImages(ArrayList<ImageIcon> images) {
		this.images = images;
	}

	/**
	 * @return the text
	 */
	public String getText() {
		return text;
	}

	/**
	 * @param text the text to set
	 */
	public void setText(String text) {
		this.text = text;
	}

	/**
	 * @return the urls
	 */
	public ArrayList<String> getUrls() {
		return urls;
	}

	/**
	 * @param urls the urls to set
	 */
	public void setUrls(ArrayList<String> urls) {
		this.urls = urls;
	}

	/**
	 * @param children the children to set
	 */
	public void setChildren(ArrayList<Idea> children) {
		this.children = children;
	}

	/**
	 * @return the changed
	 */
	public boolean isChanged() {
		return changed;
	}

	/**
	 * @param changed the changed to set
	 */
	public void setChanged(boolean changed) {
		this.changed = changed;
	}
	
	/**
	 * @return whether this node has children
	 */
	public boolean hasChildren() {
		if(children.size() > 0) {
			return true;
		}
		return false;
	}
}
